package com.mogist.hqc.module.common.ui;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mogist.hqc.R;
import com.mogist.hqc.base.BaseMVPActivity;
import com.mogist.hqc.entitys.BaseResponse;
import com.mogist.hqc.module.main.ui.MainActivity;
import com.mogist.hqc.mvp.CommonPresenter;
import com.mogist.hqc.mvp.CommonView;
import com.mogist.hqc.mvp._Presenter;
import com.mogist.hqc.view.CustomVideoView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import talex.zsw.basecore.util.PermissionConstants;
import talex.zsw.basecore.util.PermissionHelper;
import talex.zsw.basecore.util.PermissionTool;

/**
 * 作用：欢迎页
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
public class WelcomeActivity extends BaseMVPActivity<_Presenter>
	implements CommonView.View//, ViewPager.OnPageChangeListener
{
	@BindView(R.id.mVideoLayout) RelativeLayout mVideoLayout;
	@BindView(R.id.mTvClose) TextView mTvClose;
	@BindView(R.id.mIvSound) ImageView mIvSound;
	@BindView(R.id.layout) RelativeLayout layout;

	private CustomVideoView mVideoView;
	private MediaPlayer mMediaPlayer;
	private boolean sound = false;

	@Override protected void onCreate(Bundle savedInstanceState)
	{
		// 避免从桌面启动程序后，会重新实例化入口类的activity
		if(!this.isTaskRoot())
		{
			if(getIntent() != null)
			{
				String action = getIntent().getAction();
				if(getIntent().hasCategory(Intent.CATEGORY_LAUNCHER) &&
					Intent.ACTION_MAIN.equals(action))
				{
					finish();
					return;
				}
			}
		}
		super.onCreate(savedInstanceState);
	}

	@Override protected void initArgs(Intent intent)
	{
	}

	@Override protected void initView()
	{
		setContentView(R.layout.activity_welcome);
		ButterKnife.bind(this);
		mPresenter = new CommonPresenter(this);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		//当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
		{
			//透明状态栏
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}

		mTvClose.setOnClickListener(new View.OnClickListener()
		{
			@Override public void onClick(View v)
			{
				startMain();
			}
		});
		mIvSound.setOnClickListener(new View.OnClickListener()
		{
			@Override public void onClick(View v)
			{
				if(mMediaPlayer!=null)
				{
					if(sound)
					{
						sound = false;
						mMediaPlayer.setVolume(0f, 0f);
						mIvSound.setImageResource(R.drawable.i_sound_yes);
					}
					else
					{
						sound = true;
						mMediaPlayer.setVolume(1f, 1f);
						mIvSound.setImageResource(R.drawable.i_sound_no);
					}
				}
			}
		});
	}

	@Override protected void initData()
	{
		PermissionHelper.request(new PermissionTool.FullCallback()
		{
			@Override public void onGranted(List<String> permissionsGranted)
			{

			}

			@Override
			public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied)
			{

			}
		}, PermissionConstants.PHONE, PermissionConstants.STORAGE);

		if(mVideoLayout != null)
		{
			mVideoLayout.removeAllViews();
		}
		mVideoView = new CustomVideoView(getApplicationContext());
		mVideoLayout.addView(mVideoView);

		// 加载视频资源
		mVideoView.setVideoURI(Uri.parse(
			"android.resource://"+getPackageName()+"/"+R.raw.video_welcome));
		//循环播放
		mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
		{
			@Override public void onCompletion(MediaPlayer mp)
			{
				startMain();
			}
		});
		mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
			@Override public void onPrepared(MediaPlayer mp)
			{
				mMediaPlayer = mp;
				mp.setVolume(0f, 0f);
				mp.start();
			}
		});
	}

	@Override public void onDestroy()
	{
		super.onDestroy();
		if(mVideoView != null)
		{
			mVideoView.stopPlayback();
			mVideoView.setOnCompletionListener(null);
			mVideoView.setOnPreparedListener(null);
		}
		mVideoLayout.removeAllViews();
		mVideoView = null;
	}

	@Override public void bindData(BaseResponse response)
	{

	}

	private void startMain()
	{
		start(MainActivity.class);
		finish();
	}
}
